package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.item.entity.Category;
import com.leyou.item.entity.CategoryBrand;
import com.leyou.item.mapper.CategoryMapper;
import com.leyou.item.service.CategoryBrandService;
import com.leyou.item.service.CategoryService;
import com.leyou.item.dto.CategoryDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private CategoryBrandService categoryBrandService;

    @Override
    public List<CategoryDTO> queryCategoryByBrandId(Long brandId) {

        //查询中间表 select category_id from tb_category_brand where brand_id = 15127
        //1.根据品牌id,查询中间表,得到中间表对象集合
        List<CategoryBrand> categoryBrandList = categoryBrandService
                .query().eq("brand_id", brandId).list();
        //2.获取分类id集合
        List<Long> idList = categoryBrandList.stream()
                .map(CategoryBrand::getCategoryId).collect(Collectors.toList());
        //3.根据分类id集合,查询分类对象集合
        List<Category> categoryList = listByIds(idList);
        //4.转换DTO
        return CategoryDTO.convertEntityList(categoryList);
    }
}
